package com.lianjia.analysis.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.List;

/**
 * Web MVC 配置类
 * 配置消息转换器，确保返回的JSON数据不包含Java类型信息
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 配置消息转换器
     * 确保返回的JSON数据不包含Java类型信息
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 查找并配置Jackson消息转换器
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;
                ObjectMapper objectMapper = jacksonConverter.getObjectMapper();
                
                // 禁用默认类型信息
                objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
                objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                
                // 将配置好的ObjectMapper设置回转换器
                jacksonConverter.setObjectMapper(objectMapper);
                break;
            }
        }
    }
} 